
#import "ImageDownloaderASI.h"
#import "ASIHttpRequest.h"


@implementation ImageDownloaderASI

//@synthesize _Connection;
@synthesize _sURL;

-(id)init
{
	self = [super init];
	if(self)
	{
		_oResult = [[NSMutableData alloc] initWithCapacity:128];
		_bCanceled = NO;
	}
	return self;
}

-(void)dealloc
{
	if(_ImageDownloaderDelegate)
		[_ImageDownloaderDelegate release];
	self._sURL = nil;
//	self._Connection = nil;
	[_oResult  release];
	[super dealloc];
}


- (void)getImageFromURL:(NSString*)imageURL delegate:(id <ImageDownloaderASIDelegate>)dlgt
{
	[self retain];
	_ImageDownloaderDelegate = [dlgt retain];
	self._sURL = imageURL;

    NSURL *url=[NSURL URLWithString:imageURL];
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    //        [request setDownloadProgressDelegate:self.progressView];
    [request setValidatesSecureCertificate:NO];
    request.shouldAttemptPersistentConnection   = NO;
    [request setDelegate:self];
    [request setShowAccurateProgress:YES];
    [request startAsynchronous];
    
//    [m_pIndicator startAnimating];  

}

#pragma mark -  
#pragma mark ASI HTTP Request Methods  
//- (void)requestFinished:(ASIHTTPRequest *)request  
//{  
//    NSLog(@"UIAsyncImageView: the web image has been loaded completely/n");  
//    
////    [m_pIndicator stopAnimating];  
////    m_bIsLoaded = YES;  
//    
//    UIImage *image = [UIImage imageWithData:[request responseData]];  
////    _oResult = [NSMutableData dataWithData:[request responseData]];
//    [_ImageDownloaderDelegate receivedImageASI:image sender:self];
////    [m_pImageView setImage:image];  
//    
////    m_pImageView.contentMode = UIViewContentModeScaleAspectFit;  
////    m_pImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);  
//    
////    m_pImageView.frame = self.bounds;  
////    [m_pImageView setNeedsLayout];  
////    [self setNeedsLayout];  
//}  

-(void) requestFinished:(ASIHTTPRequest *)request{
	if ([_oResult length]==0) {
		return ;
	}
//	progressLabel.text=[NSString stringWithFormat:@"100％"];
//	NSString *str=[request responseString];
	UIImage *img=[[UIImage alloc] initWithData:_oResult];
    [_ImageDownloaderDelegate receivedImageASI:img sender:self];
//	imageView.image=img;
	[img release];
//	NSLog(@"=========%@",str);
}

-(void) requestFailed:(ASIHTTPRequest *)request{
	NSString *error=[[request error] description];
	NSLog(@"requestFailed=%@",error);
}

-(void) request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data{
	[_oResult appendData:data];
//	float f= progressView.progress;
//	NSLog(@"=====%f",f);
//	progressLabel.text=[NSString stringWithFormat:@"%d％",(int)(f*100)];
	
//	NSLog(@"didReceiveData");
}

-(void) request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{
	if (_oResult==nil) {
		_oResult=[[NSMutableData data] retain];
	}
	[_oResult setLength:0];
//	NSLog(@"didReceiveResponseHeaders==%@",[responseHeaders allKeys]);
	
}



- (void)cancel
{
	_bCanceled = YES;
    
	[_ImageDownloaderDelegate receivedImageASI:nil sender:self]; 
}

- (BOOL)canceled
{
	return _bCanceled;
}


@end
